<?php
/*
 * @Author: your name
 * @Date: 2020-12-14 14:02:35
 * @LastEditTime: 2020-12-18 16:02:48
 * @LastEditors: Please set LastEditors
 * @Description: 上传回滚规则
 * @FilePath: /special/application/api/service/netfilter/UploadResetFirewallRuleService.php
 */
namespace app\api\service\netfilter;

use app\adminapiv3\service\module_manage\netfilter\RuleLogService;
use app\common\definition\netfilter\FirewallRuleDefinition;
use app\common\exception\CustomException;
use think\Db;

class UploadResetFirewallRuleService extends FirewallRuleUploadService
{
    /**
     * @name: 上传
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-14 14:03:32
     */
    public function upload ()
    {
        $save_data = Db::name('netfilter_firewall_rule_log')->where('type', FirewallRuleDefinition::NORMAL_RULE_GROUP)->where('host_id', $this->host_id)->where('create_time', $this->upload_create_time)->find();

        if (empty($save_data))
        {
            throw new CustomException('未找到指定回滚规则', 422);
        }

        //如果规则没有回档无法验证规则正确性,不能回滚规则
        if ($save_data['status'] != FirewallRuleDefinition::ARCHIVED_LOG_STATUS)
        {
            throw new CustomException('该规则还未完成验证,无法回滚', 422);
        }

        $this->upload_create_time = $this->nowtime;
        $this->preview_content = json_decode($save_data['preview_content'], true);

        //定义数据源容器
        $source_a = [];
        //自定义规则数据容器
        $handle_rule_data = [];
        $RuleLogService = new RuleLogService();
        //获取回滚操作数据
        $RuleLogService->getResetOperationData($this->preview_content, $source_a, $handle_rule_data, $this->host_os);
        //重置规则数据库操作
        $RuleLogService->resetRuleDatabaseOperation($this->host_id, $handle_rule_data, $source_a, $this->preview_content);
        
        $this->archivedLog();
    }
}